export default class turntable extends Laya.Script {

    constructor() {
        super();
        /** @prop {name:intType, tips:"整数类型示例", type:Int, default:1000}*/
        let intType = 1000;
        /** @prop {name:numType, tips:"数字类型示例", type:Number, default:1000}*/
        let numType = 1000;
        /** @prop {name:strType, tips:"字符串类型示例", type:String, default:"hello laya"}*/
        let strType = "hello laya";
        /** @prop {name:boolType, tips:"布尔类型示例", type:Bool, default:true}*/
        let boolType = true;
        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0

         /** @prop {name:PrizePanel, tips:"奖品页面", type:Node}*/
         let PrizePanel;
          /** @prop {name:prizeIntroduce, tips:"奖品说明", type:Node}*/
          let prizeIntroduce;
           /** @prop {name:closePrizePanel, tips:"关闭奖品页面", type:Node}*/
         let closePrizePanel;
    }
    onStart() {

        //转盘
        let _bgPanel = this.owner.getChildByName("background");
        this.turntable = _bgPanel.getChildByName("turntable");
        //开始按钮
        this.startDrawing = _bgPanel.getChildByName("startDrawing");
        this.startDrawing.on(Laya.Event.CLICK, this, this.startDrawingBtnClick);
        this.IsLuckDrawIn = false;//是否在抽奖中 不在

        this.rotatingSpeed = 30;//旋转的速度
        this.rotationValue = 1;//旋转的值
        this.IsRotationalAcceleration = true;//是否加速旋转
        
        //关闭奖励页面的按钮
        this.closePrizePanel.on(Laya.Event.CLICK, this, this.closePrizePanelBtnClick);

        //奖池
        this.prizeList = [
            {
                number: 1,
                name: "大量钻石",
                probability: 5
            }, {
                number: 2,
                name: "下次3倍",
                probability: 5
            }, {
                number: 3,
                name: "小量钻石",
                probability: 20
            }, {
                number: 4,
                name: "再来一次",
                probability: 5
            },{
                number: 5,
                name: "少量金钱",
                probability: 40
            },
            {
                number: 6,
                name: "下次6倍",
                probability: 4
            },{
                number: 7,
                name: "大量金钱",
                probability: 20
            },{
                number: 8,
                name: "限时车辆",
                probability: 1
            },
        ];

    }
    turntableReset() {
        //console.log("角度归0");
        this.turntable.rotation = 0;
    }

    onEnable() {
    }

    onDisable() {
    }
    //随机奖品
    randomPrizes() {
        let _num = 0;
        let _prizeRandom = Math.floor(Math.random() * 100);
        for (let i = 0; i < this.prizeList.length; i++) {
            let _pl = this.prizeList[i];

            _num += _pl.probability;
            if (_num >= _prizeRandom) {
                return _pl;
            }
        }
    }
    //开始事件
    startDrawingBtnClick() {
        //在抽奖中 return
        if (this.IsLuckDrawIn == true) return;
        console.log("旋转");
        this.prize = this.randomPrizes();//奖品
        this.prizeNumer = this.prize.number;
        this.prizeRotation = (this.prizeNumer - 1) * (360/ this.prizeList.length) + 360 / this.prizeList.length / 2 + 10 * (Math.random() * 2 - 1);
        console.log("奖品", this.prizeNumer, "  角度 ", this.prizeRotation);

        this.rotatingSpeed = 70;//旋转的速度
        this.rotationValue = 5;//旋转的值
        this.IsRotationalAcceleration = true;//是否加速旋转
        this.IsEndRotation = false;//是否结束旋转

        this.IsLuckDrawIn = true;//是否在抽奖中 在
        Laya.timer.loop(100, this, this.createRect);//定时器  时间(毫秒) this自己(指定的物体)  this.createRect执行函数
    }
    createRect() {

        this.turntable.rotation += this.rotatingSpeed;
        this.turntable.rotation = this.turntable.rotation % 360;
        // console.log("角度", this.turntable.rotation);
        this.rotatingSpeed += this.rotationValue;
        // console.log("速度", this.rotatingSpeed);
        if (this.rotatingSpeed >= 120 && this.IsRotationalAcceleration == true) {
            this.IsRotationalAcceleration = false;
            this.rotationValue = -3 - Math.floor(Math.random() * 2);
        }
        if (this.rotatingSpeed <= 50 && this.IsEndRotation == false) {
            this.rotationValue = 0;
            if (this.turntable.rotation <= this.prizeRotation) {
                this.IsEndRotation = true;
            }
        }

        if (this.IsEndRotation == true && (this.prizeRotation <= this.turntable.rotation)) {
            this.turntable.rotation = this.prizeRotation;
            let _num = this.turntable.rotation;
            let number = Math.ceil(_num / 90);
                  
            Laya.timer.clear(this, this.createRect);//清除指定计时器  this（有计时器的物体）  要取消的函数
            
            Laya.timer.once(1000, this, function () {
                this.IsLuckDrawIn = false;//是否在抽奖中 不在
                this.prizeRewardPage();//1秒后执行只执行一次
            });

        }
    }
    //开启奖品奖励页面
    prizeRewardPage(){
        this.PrizePanel.visible=true;
        this.prizeIntroduce.text=this.prize.name;
    }
    //关闭奖品奖励页面
    closePrizePanelBtnClick(){
        this.PrizePanel.visible=false;
    }
}